package com.bestcem.xm.user.grpc.service;

import com.bestcem.bp.xm.user.grpc.v1.services.*;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.user.grpc.service.converter.OrgWalletConfigGrpcConverter;
import com.bestcem.xm.user.service.OrganizationWalletConfigService;
import com.bestcem.xm.user.service.dto.organization.OrganizationWalletConfigDTO;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 公司钱包配置grpc服务
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/5/13 11:01
 */
@Slf4j
@GrpcService
public class OrganizationWalletConfigGrpcService extends OrgWalletConfigServiceGrpc.OrgWalletConfigServiceImplBase {

    @Resource
    private OrganizationWalletConfigService organizationWalletConfigService;

    /**
     * 根据公司id查询公司钱包配置
     *
     * @param request          请求参数
     * @param responseObserver 响应
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/5/13 11:03
     */
    @Override
    public void getOrgWalletConfig(GetOrgWalletConfigRequest request, StreamObserver<GetOrgWalletConfigResponse> responseObserver) {
        // 根据公司id查询公司钱包配置
        ServiceResult<OrganizationWalletConfigDTO> serviceResult = organizationWalletConfigService.getByOrgId(request.getOrgId());

        // 根据结果设置返回值
        GetOrgWalletConfigResponse.Builder builder = GetOrgWalletConfigResponse.newBuilder();
        if (serviceResult.isSuccess()) {
            // 查询成功
            builder.setStatus(RpcStatusResult.success());
            builder.setConfig(OrgWalletConfigGrpcConverter.dto2GetOrgWalletConfigGrpc(serviceResult.getData()));
        } else {
            // 查询失败
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 批量查询公司钱包配置
     *
     * @param request          请求参数
     * @param responseObserver 响应
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/5/18 17:00
     */
    @Override
    public void listOrgWalletConfigs(ListOrgWalletConfigsRequest request, StreamObserver<ListOrgWalletConfigsResponse> responseObserver) {
        // 根据公司id查询公司钱包配置
        ServiceResult<List<OrganizationWalletConfigDTO>> serviceResult = organizationWalletConfigService.listByOrgIds(request.getOrgIdsList());

        // 根据结果设置返回值
        ListOrgWalletConfigsResponse.Builder builder = ListOrgWalletConfigsResponse.newBuilder();
        if (serviceResult.isSuccess()) {
            // 查询成功
            builder.setStatus(RpcStatusResult.success());
            builder.addAllConfigs(OrgWalletConfigGrpcConverter.dtos2GetOrgWalletConfigGrpcs(serviceResult.getData()));
        } else {
            // 查询失败
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 创建公司钱包配置
     *
     * @param request          请求参数
     * @param responseObserver 响应
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/5/13 11:03
     */
    @Override
    public void createOrgWalletConfig(CreateOrgWalletConfigRequest request, StreamObserver<CreateOrgWalletConfigResponse> responseObserver) {
        // 创建公司钱包配置
        ServiceResult<String> serviceResult = organizationWalletConfigService.insert(OrgWalletConfigGrpcConverter.createOrgWalletConfigGrpc2Dto(request));

        // 根据结果设置返回值
        CreateOrgWalletConfigResponse.Builder builder = CreateOrgWalletConfigResponse.newBuilder();
        if (serviceResult.isSuccess()) {
            // 查询成功
            builder.setStatus(RpcStatusResult.success());
            builder.setId(serviceResult.getData());
        } else {
            // 查询失败
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    /**
     * 根据公司id更新公司钱包配置
     *
     * @param request          请求参数
     * @param responseObserver 响应
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/5/13 11:03
     */
    @Override
    public void updateOrgWalletConfig(UpdateOrgWalletConfigRequest request, StreamObserver<UpdateOrgWalletConfigResponse> responseObserver) {
        // 根据公司id更新公司钱包配置
        ServiceResult<Integer> serviceResult = organizationWalletConfigService.updateByOrgId(OrgWalletConfigGrpcConverter.updateOrgWalletConfigGrpc2Dto(request));

        // 根据结果设置返回值
        UpdateOrgWalletConfigResponse.Builder builder = UpdateOrgWalletConfigResponse.newBuilder();
        if (serviceResult.isSuccess()) {
            // 查询成功
            builder.setStatus(RpcStatusResult.success());
        } else {
            // 查询失败
            builder.setStatus(RpcStatusResult.error(serviceResult.getMsg()));
        }
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

}
